package com.sheng.leetcode.year2023.month07.day03;

import lombok.Data;
import org.junit.Test;

import java.math.BigDecimal;

/**
 * @author liusheng
 * @date 2023/07/03
 * <p>
 * 445. 两数相加 II<p>
 * <p>
 * 给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。<p>
 * 你可以假设除了数字 0 之外，这两个数字都不会以零开头。<p>
 * <p>
 * 示例1：<p>
 * 输入：l1 = [7,2,4,3], l2 = [5,6,4]<p>
 * 输出：[7,8,0,7]<p>
 * <p>
 * 示例2：<p>
 * 输入：l1 = [2,4,3], l2 = [5,6,4]<p>
 * 输出：[8,0,7]<p>
 * <p>
 * 示例3：<p>
 * 输入：l1 = [0], l2 = [0]<p>
 * 输出：[0]<p>
 * <p>
 * 提示：<p>
 * 链表的长度范围为 [1, 100]<p>
 * 0 <= node.val <= 9<p>
 * 输入数据保证链表代表的数字无前导 0<p>
 * <p>
 * 来源：力扣（LeetCode）<p>
 * 链接：<a href="https://leetcode.cn/problems/add-two-numbers-ii">445. 两数相加 II</a><p>
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。<p>
 */
public class LeetCode445 {

    @Test
    public void test01() {
//        ListNode l1 = new ListNode(7, new ListNode(2, new ListNode(4, new ListNode(3))));
//        ListNode l2 = new ListNode(5, new ListNode(6, new ListNode(4)));
//        ListNode l1 = new ListNode(2, new ListNode(4, new ListNode(3)));
//        ListNode l2 = new ListNode(5, new ListNode(6, new ListNode(4)));
        ListNode l1 = new ListNode(0);
        ListNode l2 = new ListNode(0);
        System.out.println(new Solution().addTwoNumbers(l1, l2));
    }
}

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        String s1 = get(l1);
        String s2 = get(l2);
        String string = new BigDecimal(s1).add(new BigDecimal(s2)).toString();
        ListNode node = null;
        for (int i = string.length() - 1; i >= 0; i--) {
            node = new ListNode(Integer.parseInt(string.charAt(i) + ""), node);

        }
        return node;
    }

    public String get(ListNode root) {
        StringBuilder str = new StringBuilder();
        while (root.next != null) {
            str.append(root.val);
            root = root.next;
        }
        str.append(root.val);
        return str.toString();
    }
}

// Definition for singly-linked list.
@Data
class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

